package org.springblade.modules.notice.core.conditon.impl;

import org.springblade.modules.notice.core.conditon.TriggerConditionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author ysq
 * @Date 2024/12/06
 * @Description 当维修任务开始后，预计维修结束时间前5分钟，还未签退，则发送通知
 */
@Component
public class Condition3Handler implements TriggerConditionFactory {

	@Resource
	private JdbcTemplate jdbcTemplate;

	/**
	 * 这里为了校验预计完成时间有无调整过，所以加了70秒的容差
	 */
	private static final String C_3_SQL = "SELECT IF( t1.id > 0, 1, 0 ) FROM blade_maintain_plan_content t1 \n" +
		"WHERE t1.status = 3 AND ABS(TIMESTAMPDIFF( SECOND, NOW() + INTERVAL 5 MINUTE, t1.finish_date )) <=70 AND t1.id = ?";

	@Override
	public boolean isTrigger(Long bizId) {
		try {
			return jdbcTemplate.queryForObject(C_3_SQL, new Object[]{bizId}, boolean.class);
		} catch (DataAccessException e) {
			return false;
		}
	}
}
